package crp.core.portal.domain.services;

import com.qwlabs.panache.Ranged;
import crp.core.portal.domain.models.NewsArticleEntity;
import crp.core.portal.domain.models.NewsArticleQuery;
import crp.core.portal.domain.repositories.NewsArticleRepository;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.Optional;

@ApplicationScoped
public class NewsArticleSearcher {
    private final NewsArticleRepository newsArticleRepository;

    @Inject
    public NewsArticleSearcher(NewsArticleRepository newsArticleRepository) {
        this.newsArticleRepository = newsArticleRepository;
    }

    public Ranged<NewsArticleEntity> by(NewsArticleQuery query) {
        return Ranged.of(newsArticleRepository.by(query), query.getRange());
    }

    public Optional<NewsArticleEntity> byId(String id) {
        return (newsArticleRepository.findByIdOptional(id));
    }
}
